package com.yoyosys.common.util;


import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 常用工具类
 * @author Meron
 *
 */
@Slf4j
public class CommonUtil {
	public static String UTF8 = "UTF8";
	public static final SimpleDateFormat FORMAT_DATETIME =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat FORMAT_DATE =  new SimpleDateFormat("yyyy-MM-dd");

	/**
	 * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
	 *
	 * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
	 *
	 * <pre>
	 * isBlank(null)      = true
	 * isBlank("")        = true
	 * isBlank(" ")       = true
	 * isBlank("bob")     = false
	 * isBlank("  bob  ") = false
	 * </pre>
	 *
	 * @param cs  the CharSequence to check, may be null
	 * @return {@code true} if the CharSequence is null, empty or whitespace only
	 * @since 2.0
	 * @since 3.0 Changed signature from isBlank(String) to isBlank(CharSequence)
	 */
	public static boolean isBlank(final CharSequence cs) {
		int strLen;
		if (cs == null || (strLen = cs.length()) == 0) {
			return true;
		}
		for (int i = 0; i < strLen; i++) {
			if (Character.isWhitespace(cs.charAt(i)) == false) {
				return false;
			}
		}

		if("null".contentEquals(cs)){
			return true;
		}

		return true;
	}

	/**
	 * <p>Checks if a CharSequence is not empty (""), not null and not whitespace only.</p>
	 *
	 * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
	 *
	 * <pre>
	 * isNotBlank(null)      = false
	 * isNotBlank("")        = false
	 * isNotBlank(" ")       = false2022-11-10
	 * isNotBlank("bob")     = true
	 * isNotBlank("  bob  ") = true
	 * </pre>
	 *
	 * @param cs  the CharSequence to check, may be null
	 * @return {@code true} if the CharSequence is
	 *  not empty and not null and not whitespace only
	 * @since 2.0
	 * @since 3.0 Changed signature from isNotBlank(String) to isNotBlank(CharSequence)
	 */
	public static boolean isNotBlank(final CharSequence cs) {
		return !isBlank(cs);
	}

	/**
	 * 取得参数列表中第一个不为空的值
	 * @param strings
	 */
	public static String getFirstNotBlank(Object ...strings){
		for(Object str : strings){
			if(str != null && isNotBlank(String.valueOf(str))){
				return String.valueOf(str);
			}
		}
		return "";
	}
	/**
	 * 取得参数列表中第一个不为空的值
	 * @param objs
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static <T> T  getFirstNotNull(T ...objs){
		for(Object obj : objs){
			if(obj != null){
				return (T)obj;
			}
		}
		return null;
	}
	/**
	 * 如果in为null返回out,否则返回in
	 * @param in
	 * @param out
	 * @return
	 */
	public static <T> T ifNull(T in, T out){
		if(in==null){
			return out;
		}
		return in;
	}

	/**
	 * 取得参数列表中第一个不为空的值
	 * @param ids
	 * @return
	 */
	public static Integer getFirstNotNull0(Integer ...ids){
		for(Integer id : ids){
			if(isNotNull0(id)){
				return id;
			}
		}
		return null;
	}
	/**
	 * <p>Joins the elements of the provided {@code Iterator} into
	 * a single String containing the provided elements.</p>
	 *
	 * <p>No delimiter is added before or after the list.
	 * A {@code null} separator is the same as an empty String ("").</p>
	 *
	 * <p>See the examples here: {@link #join(Object[],String)}. </p>
	 *
	 * @param iterator  the {@code Iterator} of values to join together, may be null
	 * @param separator  the separator character to use, null treated as ""
	 * @return the joined String, {@code null} if null iterator input
	 */
	public static String join(final Iterator<?> iterator, final String separator) {

		// handle null, zero and one elements before building a buffer
		if (iterator == null) {
			return null;
		}
		if (!iterator.hasNext()) {
			return "";
		}
		final Object first = iterator.next();
		if (!iterator.hasNext()) {
			final String result = Objects.toString(first, "");
			return result;
		}

		// two or more elements
		final StringBuilder buf = new StringBuilder(256); // Java default is 16, probably too small
		if (first != null) {
			buf.append(first);
		}

		while (iterator.hasNext()) {
			if (separator != null) {
				buf.append(separator);
			}
			final Object obj = iterator.next();
			if (obj != null) {
				buf.append(obj);
			}
		}
		return buf.toString();
	}


	/**
	 * 判断两个list是否是相同的list(忽略顺序,去除空元素,忽略重复元素)
	 * @param list1
	 * @param list2
	 */
	public static boolean isSameList(String list1, String list2, String spilter){
		if(isBlank(list1) || isBlank(list2)){
			return false;
		}
		list1 = list1.trim();
		list2 = list2.trim();
		if(list1.equals(list2)){
			return true;
		}
		Set<String> set1 = getStrSet(list1, spilter);
		Set<String> set2 = getStrSet(list2, spilter);

		return set1.equals(set2);
	}
	/**
	 *
	 * @param strs - 逗号分隔的字符串列表
	 * @param spilter - 分割符
	 * @return
	 */
	public static Set<String> getStrSet(String strs, String spilter){
		Set<String> set = new HashSet<String>();
		if(isNotBlank(strs)){
			String[] strArray = strs.trim().split(spilter);
			for (int i = 0; i < strArray.length; i++) {
				String thisStr = strArray[i];
				if(isNotBlank(thisStr)){
					set.add(thisStr.trim());
				}
			}
		}
		return set;
	}
	/**
	 * 合并两个逗号list,去除重复元素
	 * @param list1
	 * @param list2
	 * @return
	 */
	public static String mergeList(String list1, String list2){
		if(isBlank(list1) && isBlank(list2)){
			return "";
		}

		String list = ifNull(list1, "") +","+ ifNull(list2, "");
		Set<String> set = new HashSet<String>();
		String[] strArray = list.trim().split(",");
		for (int i=0; i<strArray.length; i++) {
			String thisStr = strArray[i];
			if(isNotBlank(thisStr)){
				set.add(thisStr.trim());
			}
		}
		if(set.size() == 0 ){
			return "";
		}
		return join(set, ",");
	}

	/**
	 * 连接成字符串
	 * @param objs
	 * @return
	 */
	public static String join(Object...objs){
		StringBuilder sb = new StringBuilder();
		for(Object obj : objs){
			sb.append(obj);
		}
		return sb.toString();
	}
	/**
	 * 用connector连接成字符串，并且忽略空串，去除空白
	 * @param connector
	 * @param strings
	 * @return
	 */
	public static String joinWith(char connector, String...strings){
		StringBuilder sb = new StringBuilder();
		for(String string : strings){
			if(isNotBlank(string)){
				sb.append(connector).append(string.trim());
			}
		}
		return sb.toString().replaceAll("^"+connector+"+|"+connector+"+$", "");
	}
	 /**
	 * 用connector连接成字符串，并且忽略空串，去除空白
	 * @param connector
	 * @param objs
	 * @return
	 */
	public static String joinWith(String connector, Object...objs){
		StringBuilder sb = new StringBuilder();
		for(Object obj : objs){
			if(obj != null){
				sb.append(connector).append(String.valueOf(obj).trim());
			}
		}
        return sb.toString().replaceAll("^"+connector+"+|"+connector+"+$", "");
	}
	public static boolean isInArray(Integer[] ids, Integer id){
		if(ids==null || id==null){
			return false;
		}
		for(Integer thisId : ids){
			if(isEqual(thisId, id)){
				return true;
			}
		}
		return false;
	}

	public static boolean isInArray(String[] arr, String str){
		if(arr==null || str==null){
			return false;
		}
		for(String thisStr : arr){
			if(thisStr == null){
				continue;
			}
			if(thisStr.trim().equals(str.trim())){
				return true;
			}
		}
		return false;
	}
	public static boolean isInList(String idsStr, Integer id){
		if(idsStr==null || id==null){
			return false;
		}
		return join(",",idsStr,",").indexOf(join(",",id,",")) > -1;
	}
	public static boolean isInList(String list, String str){
		if(list==null || str==null){
			return false;
		}
		String[] arr = list.split(",");
		return isInArray(arr, str);
	}

	/**
	 * 判断两个Integer型的对象是否相等
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isEqual(BigDecimal a, BigDecimal  b){
		if(a == null || b == null){
			return false;
		}
		return (a.compareTo(b) == 0);
	}
	/**
	 * 判断两个Integer型的对象是否相等
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isEqual(Integer a, Integer b){
		if(a == null || b == null){
			return false;
		}
		return (a.compareTo(b) == 0);
	}
	/**
	 * 判断两个Long型的对象是否相等
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isEqual(Long a, Long b){
		if(a == null || b == null){
			return false;
		}
		return (a.compareTo(b) == 0);
	}
	/**
	 * 判断两个Double型的对象是否相等
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isEqual(Double a, Double b){
		if(a == null || b == null){
			return false;
		}
		return (a.compareTo(b) == 0);
	}

	public static boolean isNotEqual(Integer a, Integer b){
		return !isEqual(a,b);
	}
	public static boolean isNotEqual(Long a, Long b){
		return !isEqual(a,b);
	}
	public static boolean isNotEqual(Double a, Double b){
		return !isEqual(a,b);
	}
	/**
	 * 为null或者为0
	 * @param a
	 * @return
	 */
	public static boolean isNull0(Integer a){
		if(a == null){
			return true;
		}
		return (a.compareTo(0) == 0);
	}
	public static boolean isNull0(Long a){
		if(a == null){
			return true;
		}
		return (a.compareTo(0l) == 0);
	}
	/**
	 * 不为null且不为0
	 */
	public static boolean isNotNull0(Integer a){
		if(a == null){
			return false;
		}
		return (a.compareTo(0) != 0);
	}
	/**
	 * 不为null且不为0
	 */
	public static boolean isNotNull0(BigDecimal a){
		if(a == null){
			return false;
		}
		return (a.compareTo(new BigDecimal(0)) != 0);
	}
	public static boolean isNotNull0(Long a){
		if(a == null){
			return false;
		}
		return (a.compareTo(0l) != 0);
	}
	public static boolean isNotNull0(Float a){
		if(a == null){
			return false;
		}
		return (a.compareTo(0f) != 0);
	}
	public static boolean isNull0(Float a){
		if(a == null){
			return true;
		}
		return (a.compareTo(0f) == 0);
	}

	public static boolean isNull0(BigDecimal a){
		if(a == null){
			return true;
		}
		return (a.compareTo(new BigDecimal(0)) == 0);
	}

	/**
	 * 解析字典的code为名称
	 * <pre>如果code不在codeList中：
	 * 		- 如果nameList的元素数  > codeList的元素，则返回nameList的最后一个，视其为默认值
	 * 		- 如果nameList的元素数 <= codeList的元素，则返回 code本身
	 *其他特殊情况，返回code本身
	 * </pre>
	 * @param code, 如: 1
	 * @param codeList, 如: 1,2
	 * @param nameList, 如: 家长,教师
	 * @return
	 */
	public static String transDic(Object code, String codeList, String nameList) {
		String str = null;
		if(code==null){
			str = "";
		} else {
			str = String.valueOf(code);
		}
		if(isBlank(codeList) || isBlank(nameList)){
			return "";
		}
		if(isBlank(str)){
			str = "";
		}
		str = trim(str);
		String[] codeParts = codeList.split("[,，]");
		String[] nameParts = nameList.split("[,，]");
		for(int i=0; i<codeParts.length; i++){
			if(str.equals(trim(codeParts[i]))){
				if(nameParts.length>i){
					return trim(nameParts[i]);
				} else {//code列表长度和比name列表长度大
					return str;
				}
			}
		}
		//到此则codeList中没有目标code
		if(codeParts.length < nameParts.length){//nameList较长，则认为其最后一个为默认值
			return trim(nameParts[nameParts.length-1]);
		}
		//返回原值
		return str;

	}
	/**
	 * value 转为整型，如果value是浮点型的，直接去掉末尾
	 * @param value
	 * @return
	 */
	public static Integer toInteger(Object value){
		return toInteger(value, null);
	}
	public static Integer toInteger(Object value, Integer defaultValue){
		if(value == null){
			return defaultValue;
		}
		Integer result = null;
		try{
			result = (Integer)value;
			return result;
		}catch(ClassCastException cce){
			String clazz = value.getClass().getSimpleName();
			if("Double".equals(clazz)){
				result = ((Double)value).intValue();
				return result;
			} else if("Float".equals(clazz)){
				result = ((Float)value).intValue();
				return result;
			} else if("BigDecimal".equals(clazz)){
				result = ((BigDecimal)value).intValue();
				return result;
			} else {
				try{
					result = Integer.parseInt(value.toString().trim().replaceAll("\\..*$", ""));
					return result;
				} catch(Exception e){
				    log.error("toInteger error1, value="+value, e);
				}
			}
		}catch(Exception e){
            log.error("toInteger error2, value="+value, e);
		}
		return defaultValue;
	}
	public static Float toFloat(Object value){
		return toFloat(value, null);
	}
	public static Float toFloat(Object value, Float defaultValue){
		if(value == null){
			return defaultValue;
		}
		Float result = defaultValue;
		try{
			result = (Float)value;
			return result;
		}catch(ClassCastException cce){
			try{
				result = Float.parseFloat(value.toString().trim());
				return result;
			} catch(Exception e){
				e.printStackTrace();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return defaultValue;
	}
	public static Double toDouble(Object value){
		return toDouble(value, null);
	}
	public static Double toDouble(Object value, Double defaultValue){
		if(value == null){
			return defaultValue;
		}
		Double result = defaultValue;
		try{
			 String valueClass = value.getClass().getSimpleName();
			//基本类型数据
			if("Double".indexOf(valueClass) != -1){
				result = (Double)value;
			} else {
				result = Double.parseDouble(value.toString().trim());
			}
			return result;

		}catch(ClassCastException cce){
			try{
				result = Double.parseDouble(value.toString().trim());
				return result;
			} catch(Exception e){
                log.error("toDouble error1, value="+value, e);
			}
		}catch(Exception e){
            log.error("toDouble error2, value="+value, e);
		}
		return defaultValue;
	}
    public static Short toShort(Object value){
        return toShort(value, null);
    }
    public static Short toShort(Object value, Short defaultValue){
        if(value == null){
            return defaultValue;
        }
        Short result = defaultValue;
        try{
            result = (Short)value;
            return result;
        }catch(ClassCastException cce){
            try{
                result = Short.parseShort(value.toString().trim());
                return result;
            } catch(Exception e){
                log.error("toShort error1, value="+value, e);
            }
        }catch(Exception e){
            log.error("toShort error2, value="+value, e);
        }
        return defaultValue;
    }
	/**
	 * 获取最大值
	 * @return
	 */
	public static BigDecimal max(BigDecimal... ds){
		if(ds == null || ds.length == 0){
			return null;
		}
		BigDecimal max = ds[0];
		for(BigDecimal d : ds){
			if(d.compareTo(max) > 0){
				max = d;
			}
		}
		return max;
	}

	/**
	 * 获取最小值
	 * @return
	 */
	public static BigDecimal min(BigDecimal... ds){
		if(ds == null || ds.length == 0){
			return null;
		}
		BigDecimal min = ds[0];
		for(BigDecimal d : ds){
			if(d.compareTo(min) < 0){
				min = d;
			}
		}
		return min;
	}
    /**
     * 转为BigDecimal并保留指定小数位数
     * @param value
     * @return
     */
    public static BigDecimal toDecimal(Object value){
        return toDecimal(value, null);
    }

	/**
	 * 转为BigDecimal并保留指定小数位数
	 * @param value
	 * @param defaultValue
	 * @return
	 */
	public static BigDecimal toDecimal(Object value, BigDecimal defaultValue){
		if(value == null){
			return defaultValue;
		}
		if(value instanceof BigDecimal){
			return (BigDecimal)value;
		}
        try{
            BigDecimal decimal = new BigDecimal(String.valueOf(value));
            decimal.setScale(2);
            return decimal;
        }catch(Exception e){
            log.error("toDecimal error, value="+value, e);
            return defaultValue;
        }
	}

	public static Long toLong(Object value){
		return toLong(value, null);
	}
	public static Long toLong(Object value, Long defaultValue){
		if(value == null){
			return defaultValue;
		}
		Long result = defaultValue;
		try{
			result = (Long)value;
			return result;
		}catch(ClassCastException cce){
			try{
				result = Long.parseLong(value.toString().trim());
				return result;
			} catch(Exception e){
                log.error("toLong error1, value="+value, e);
			}
		}catch(Exception e){
            log.error("toLong error2, value="+value, e);
		}
		return defaultValue;
	}
	public static String toString(Object value){
		return toString(value, null);
	}
	public static String toString(Object value, String defaultValue){
		if(value == null){
			return defaultValue;
		}
		return String.valueOf(value).trim();
	}

	public static Boolean toBoolean(Object value){
		return toBoolean(value, null);
	}
	public static Boolean toBoolean(Object value, Boolean defaultValue){
		if(value == null){
			return defaultValue;
		}
		Boolean result = defaultValue;
		try{
			result = (Boolean)value;
			return result;
		}catch(ClassCastException cce){
			String strValue = value.toString().trim();
			if("true".equalsIgnoreCase(strValue) || "yes".equalsIgnoreCase(strValue) || "1".equalsIgnoreCase(strValue)){
				return true;
			}
			return false;
		}catch(Exception e){
            log.error("toBoolean error, value="+value, e);
		}
		return defaultValue;
	}

    public static Date toDate(Object value){
        return toDate(value, null);
    }
    /**
     *
     * @param value
     * @param format
     * @return
     */
    public static Date toDate(Object value, String format){
        if(value == null){
            return null;
        }
        if("0".equals(value+"")){
            return null;
        }
        Date result = null;
        try{
            result = (Date)value;
            return result;
        }catch(ClassCastException cce){
            String strValue = value.toString().trim();
            strValue = strValue.replaceAll("\\s+", " ").replaceAll("\\s0000$", " +0000");//ios传过来的时区信息
            if (strValue.matches("^\\d+$")) {//long
                try {
                    long timeLong = Long.parseLong(strValue);
                    return new Date(timeLong);
                } catch (Exception e) {
                    throw e;
                }
            } else {
                if(isBlank(format)){
                    DateFormat[] acceptDateFormats = {
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z"),
                            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
                            new SimpleDateFormat("yyyy-MM-dd"),
                            new SimpleDateFormat("yyyy年MM月dd日"),
                            new SimpleDateFormat("yyyy-M-d HH:m:s"),
                            new SimpleDateFormat("yyyy-MM-dd HH:mm"),
                            new SimpleDateFormat("yyyy-M-d"),
                            new SimpleDateFormat("yyyy年M月d日"),
                            new SimpleDateFormat("yyyy/MM/dd"),
                            new SimpleDateFormat("yyyy/M/d"),
                            new SimpleDateFormat("yyyy-MM"),
                            new SimpleDateFormat("yyyy-M"),
                            new SimpleDateFormat("yyyy年MM月"),
                            new SimpleDateFormat("yyyy年M月")
                    };
                    for (DateFormat f : acceptDateFormats) {
                        try {
                            return f.parse(strValue);
                        } catch (ParseException | RuntimeException e) {
                            log.error("toDate error1, value="+value, e);
                        }
                    }
                } else {
                    try {
                        return (new SimpleDateFormat(format)).parse(strValue);
                    } catch (ParseException | RuntimeException e) {
                        log.error("toDate error2, value="+value, e);
                        return null;
                    }

                }
            }//long..else
        }catch(Exception e){
            log.error("toDate error3, value="+value, e);
        }
        return null;
    }

	public static String trim(String str){
		if(str==null){
			return "";
		}
		return str.replace("　", " ").trim();
	}

	public static <E> boolean isEmpty(Collection<E> c){
		return c==null || c.isEmpty();
	}
	public static <K,V> boolean isEmpty(Map<K, V> m){
		return m==null || m.isEmpty();
	}
	public static <E> boolean isNotEmpty(Collection<E> c){
		return !isEmpty(c);
	}

	public static <K,V> boolean isNotEmpty(Map<K, V> m){
		return !isEmpty(m);
	}

	/**
	 * <pre>
	 * 替换字符串模板中的变量,变量格式{n}, n为变量序号,从0开始
	 * 如：xxxx{0}yyy{1}zzz,
	 * </pre>
	 * @param template - 字符串模板
	 * @param values - 替换后的值列表
	 * @return
	 */
	public static String format(String template, Object... values){
		String result = template;

		for(int i=0; i<values.length; i++){
			result = result.replaceAll("\\{"+i+"\\}", String.valueOf(values[i]));
		}

		return result;
	}

	public static String formatDate(Date date, String format){
		if(date == null){
			return "invalid date";
		}
		return new SimpleDateFormat(format).format(date);
	}
	public static String formatDate(Date date, SimpleDateFormat format){
		if(date == null){
			return "invalid date";
		}
		return format.format(date);
	}

	/**
	 * 将一个元素加入到唯一性列表尾部
	 * @param list
	 * @param element
	 * @return
	 */
	public static String addToList(String list, Object element){
		return addToList(list, element, true);
	}
	/**
	 * 将一个元素加入到列表中尾部
	 * @param list
	 * @param element
	 * @param unique
	 * @return
	 */
	public static String addToList(String list, Object element, boolean unique){
		return addToList(list, element, unique, true);
	}
	/**
	 * 将一个元素加入到列表中
	 * @param list
	 * @param element
	 * @param unique
	 * @param add2Tail  - 是否从队尾追加， true:队尾追加, false:队首添加
	 * @return
	 */
	public static String addToList(String list, Object element, boolean unique, boolean add2Tail){
		if(element == null){
			return list;
		}
		String str = String.valueOf(element).trim();
		if(isBlank(list)){
			return str;
		}
		list = list.trim();
		if(unique && isInList(list, str)){
		} else {
			if(add2Tail){
				list = list + "," + str;
			} else {
				list =  str + "," + list;
			}
		}

		list = list.replaceAll("\\s+", "").replaceAll("^,*", "").replaceAll(",*$", "").replaceAll(",+", ",");
		return list;
	}


	/**
	 * 将一个元素从列表中移除
	 * @param list
	 * @param element
	 * @return
	 */
	public static String removeFromList(String list, Object element){
		if(element == null || list == null){
			return list;
		}
		if(isBlank(list)){
			return list;
		}
		String str = String.valueOf(element).trim();
		if(isBlank(str)){
			return list;
		}
		list = ","+list.trim()+",";
		list = list.replaceAll(","+str+",", ",").replaceAll("\\s+", "").replaceAll("^,*", "").replaceAll(",*$", "").replaceAll(",+", ",");
		return list;
	}


    /**
     * 合并URL。 从后往前合并，如果合并完后面的部分出现完整的url，则前面的部分丢弃
     */
    public static String mergeUrl(String...parts){
        int length = parts.length;
        StringBuilder urlSb = new StringBuilder();
        for(int i=length-1; i>=0; i--){
            String part = toString(parts[i]);
            if(isBlank(part)){
                continue;
            }

            if(urlSb.length() > 0){
                urlSb.insert(0, "/");
            }
            urlSb.insert(0, part);
            if(part.matches("^http(s?)://.+$")){
                break;
            }
        }

        String url = urlSb.toString()
                .replace("://", ":##")
                .replaceAll("/+", "/")
                .replace(":##", "://");

        return url;
    }

	public static String leftPad(Object obj, int len, char padding){
		return strPad(obj, len, padding, true);
	}
	public static String rightPad(Object obj, int len, char padding){
		return strPad(obj, len, padding, false);
	}
	public static String strPad(Object obj, int len, char padding, boolean left){
		String str = obj==null? "" : String.valueOf(obj);

		int diff = len - str.length();
		if(diff <= 0){
			return str;
		}

		StringBuilder sb = new StringBuilder();
		for(int i=0; i<diff; i++){
			sb.append(padding);
		}

		if(left){
			return sb.append(str).toString();
		} else {
			return str+sb.toString();
		}
	}

	public static String formatList(String list){
		if(isBlank(list)){
			return "";
		}
		return list.replaceAll("\\s*,\\s*",",")
                .replaceAll(",+",",")
                .replaceAll("^,+|,+$","");
	}

	public static boolean listContains(String bigList, String smallList){
		if(isBlank(bigList) || isBlank(smallList)){
			return false;
		}
		String bigListPlus = ","+bigList+",";

		String[] smallArr = smallList.split(",");
		for(String thisStr : smallArr){
			if(isBlank(thisStr)){
				continue;
			}

			if(!bigListPlus.matches("^.*,\\s*"+thisStr.trim()+"\\s*,.*$")){
				return false;
			}
		}
		return true;
	}
	/**
	 * 判断两个list是否有共同元素
	 * @param list1
	 * @param list2
	 * @return
	 */
	public static boolean hasSameElement(String list1, String list2){
		if(isBlank(list1) || isBlank(list2)){
			return false;
		}
		String[] arr1 = list1.split(",");
		String list2Plus = ","+list2+",";
		for(String thisStr : arr1){
			if(isBlank(thisStr)){
				continue;
			}

			if(list2Plus.matches("^.*,\\s*"+thisStr.trim()+"\\s*,.*$")){
				return true;
			}
		}
		return false;
	}
	// 生成当前路径
	public static String buildCurrentPath(Integer num) {
		String curr = num.toString();
		StringBuffer temp = new StringBuffer("P");
		for (int i = 0; i < 5 - curr.length(); i++) {
			temp.append("0");
		}
		return temp + curr;
	}

}
